$(function () {

    // 查询
    $("#btn_xsmxdetail_search").click(function () {
        $('#grid_xsmx_detail').datagrid('load',
            {
                xsdh: $('#xsdh').val(),
                xszxh: $('#xszxh').val(),
                carNo: $('#carNo').val(),
                bindNo: $("#bindNo").val()
            }
        );
    });
    //表格初始加载
    $('#grid_xsmx_detail').datagrid({
        url: $.webPath() + '/xsgl/xsmx/xsmxDetailList?xshtId=' + $("[name='xsht_name']").val() || '',
        fitColumns: false,
        singleSelect: true,
        fit: true,
        checkbox: true,
        checkOnSelect: false,
        selectOnCheck: false,
        showFooter: true,
        toolbar: "#toolbar_xsmx_detail",
        frozenColumns: [
            [
                {field: 'xsdh', title: '销售单号', width: 110},
                {field: 'zdrq', title: '制单日期', width: 85},
                {field: 'xsxt_xsdh', title: '合同子项号', width: 85},
                {field: 'name', title: '品名', width: 50},
                {field: 'cz', title: '材质', width: 85},
                {field: 'ggxh', title: '规格', width: 85},
                {field: 'carno', title: '车皮号', width: 85},
                {field: 'bindno', title: '捆包号', width: 85},
                {field: 'dw', title: '单位', width: 30},
                {field: 'rkrq', title: '入库日期', width: 85},
                {field: 'ckrq', title: '出库日期', width: 85}
            ]
        ],
        columns: [
            [
                {
                    field: 'rkjs', title: '件数',
                    formatter: function (value) {
                        var js = formatSlForHtml(value, ",");
                        return js;
                    }, width: 70
                },
                {
                    field: 'rksl', title: '入库数量（吨）',
                    formatter: function (value) {
                        var sl = formatSlForHtml(value, ",");
                        return sl;
                    }, width: 100
                },
                {
                    field: 'sbsl', title: '剩余数量（吨）',
                    formatter: function (value) {
                        var rksl = formatSlForHtml(value, ",");
                        return rksl;
                    }, width: 100
                },
                {
                    field: 'hsj', title: '合同单价(含税)',
                    formatter: function (value) {
                        var hsj = formatJe(value, ",");
                        return hsj;
                    }, width: 100
                },
                {
                    field: 'sjjsj', title: '实际结算价',
                    formatter: function (value) {
                        var sjjsj = formatJe(value, ",");
                        return sjjsj;
                    }, width: 100
                },
                {
                    field: 'ghje', title: '购货金额',
                    formatter: function (value) {
                        var sl = formatJe(value, ",");
                        return sl;
                    }, width: 100
                },
                {
                    field: 'dlfje', title: '代理费',
                    formatter: function (value) {
                        var dlfje = formatJe(value, ",");
                        return dlfje;
                    }, width: 100
                },
                {
                    field: 'txje', title: '贴息',
                    formatter: function (value) {
                        var txje = formatJe(value, ",");
                        return txje;
                    }, width: 100
                },
                {
                    field: 'zjzyf', title: '资金占用费',
                    formatter: function (value) {
                        var zjzyf = formatJe(value, ",");
                        return zjzyf;
                    }, width: 100
                },
                {
                    field: 'shje', title: '赎货金额',
                    formatter: function (value) {
                        var shje = formatJe(value, ",");
                        return shje;
                    }, width: 100
                },
                {field: 'shsj', title: '赎货时间', width: 100},
                {field: 'bm', title: '关联收款', width: 150}
            ]
        ],
        //loadFilter: function (data) {
        //    $.each(data.rows, function () {
        //        var bhsj = Math.round(this.bhsj * 10000) / 10000;
        //        var hsj = Math.round(this.hsj * 10000) / 10000;
        //        var sl = Math.round(this.sbsl * 10000) / 10000;
        //
        //        if (this.bhsj) {
        //            this.bhse = Math.round(multiply(sl, bhsj) * 100) / 100;
        //        }
        //        if (this.hsj) {
        //            this.hse = Math.round(multiply(sl, hsj) * 100) / 100;
        //        }
        //    })
        //    return data;
        //},
        //onSelect: function (rowIndex, rowData) {
        //},
        onLoadSuccess: function () {
            countSumXsd();
        }
    });
})

//合计计算
function countSumXsd() {
    var sumrkjs = 0;
    var sumsbsl = 0;
    var sumrksl = 0;
    var sumshje = 0;
    var data = $("#grid_xsmx_detail").edatagrid("getData");
    for (var i = 0; i < data.rows.length; i++) {
        var rkjs = parseFloat(data.rows[i]["rkjs"]);
        var sbsl = parseFloat(data.rows[i]["sbsl"]);
        var rksl = parseFloat(data.rows[i]["rksl"]);
        var shje = parseFloat(data.rows[i]["shje"]);
        rkjs = isNaN(rkjs) ? 0 : rkjs;
        sbsl = isNaN(sbsl) ? 0 : sbsl;
        rksl = isNaN(rksl) ? 0 : rksl;
        shje = isNaN(shje) ? 0 : shje;
        sumrkjs += rkjs;
        sumsbsl += sbsl;
        sumrksl += rksl;
        sumshje += shje;
    }
    addFooter(sumrkjs, sumrksl, sumsbsl, sumshje);

}

function addFooter(sumrkjs, sumrksl, sumsbsl, sumshje) {
    var footer = null;
    footer = [
        {
            "ckrq": "合计", "rkjs": formatSl(sumrkjs), "rksl": formatSl(sumrksl),
            "sbsl": formatSl(sumsbsl), "shje": formatSl(sumshje)
        }
    ];
    if (footer != null)
        $("#grid_xsmx_detail").edatagrid('reloadFooter', footer);
}

/**
 * 导出赎货金额销售明细
 */
$("#xsmxExp").click(function () {
    var xsdh = $('#xsdh').val();
    var xszxh = $('#xszxh').val();
    var carNo = $('#carNo').val();
    var bindNo = $("#bindNo").val();
    var xshtId = $("[name='xsht_name']").val();
    var expUrl = $.webPath() + "/xsgl/xsmx/xsmxdetail_xsmx_exp?xsdh=" + xsdh + "&xszxh=" + xszxh + "&carNo=" + carNo + "&bindNo=" + bindNo + "&xshtId=" + xshtId;
    window.open(expUrl);
});